package com.diyunapp.xilinapp.me;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.adapters.PopListAdapter;
import com.diyunapp.xilinapp.bean.ExpressBean;
import com.diyunapp.xilinapp.bean.PopList;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.MyAsyncTask;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FillExpressNumbersActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.express_company_et)
    EditText expressCompanyEt;
    @Bind(R.id.express_numbers_et)
    EditText expressNumbersEt;
    @Bind(R.id.express_companyid_et)
    EditText express_companyid_et;
    @Bind(R.id.login_tv)
    TextView loginTv;
    private View express_parent_view;
    private List<ExpressBean.DataBean> info = new ArrayList<>();
    private MyAsyncTask asyncTask;
    private PopListAdapter bankAdapter;
    private ListView bankListView;
    private List<PopList> bankStrList;
    private int selected_bank = -1;
    private PopupWindow bankPopuWindow;
    private String Refund_id;
    private View headView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fill_express_numbers);
        ButterKnife.bind(this);
        headView = findViewById(R.id.fillex_menubar_inc);
        super.MenuWidget(headView);
        bankPopupWindow();
        Refund_id = getIntent().getStringExtra("Refund_id");
        initDiaData();
    }
    private void bankPopupWindow() {
        //设置菜单中更多功能使用的PopupWindow
        express_parent_view = (View)findViewById(R.id.dropdown_bank_view);
        LinearLayout root = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.poplistlayout, null);
        bankListView = (ListView)root.findViewById(R.id.pop_list_view);
        bankStrList = new ArrayList<PopList>();
        bankAdapter = new PopListAdapter(this, bankStrList);
        bankListView.setAdapter(bankAdapter);
        bankListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                selected_bank = position;
                expressCompanyEt.setText(bankStrList.get(position).getPop_info());

                bankPopuWindow.dismiss();
            }
        });

        bankPopuWindow = new PopupWindow(root,400, LinearLayout.LayoutParams.WRAP_CONTENT);
        // 点击外部可关闭窗口
        bankPopuWindow.setBackgroundDrawable(getDrawable());
        bankPopuWindow.setOutsideTouchable(true);
        bankPopuWindow.setFocusable(true);
    }

    private Drawable getDrawable() {
        ShapeDrawable bgdrawable =new ShapeDrawable(new OvalShape());
        bgdrawable.getPaint().setColor(getResources().getColor(android.R.color.transparent));
        return bgdrawable;
    }

    private void initDiaData() {
        String url=""+MainActivity.url+"act=goods&op=express";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                        if(result==200){
                            JSONArray data=resultObject.optJSONArray("data");
                            for (int i=0;i<data.length();i++){
                                JSONObject provinceJson = data.getJSONObject(i);
                                String id = provinceJson.getString("id");
                                String name = provinceJson.getString("name");
                                bankStrList.add(new PopList(Integer.parseInt(id), name));
                            }
                            bankAdapter.notifyDataSetChanged();

                        }

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {


                    }
                });
    }


    @OnClick({R.id.login_tv,R.id.express_company_et,R.id.go_back})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.express_company_et:
                bankPopuWindow.showAsDropDown(express_parent_view);
                break;
            case R.id.login_tv:
                if(TextUtils.isEmpty(expressCompanyEt.getText().toString())){
                    toast("请输入快递公司");
                }else if(TextUtils.isEmpty(expressNumbersEt.getText().toString())){
                    toast("请输入快递单号");
                }else{
                    commitExpress();
                    setResult(2);
                    finish();
//                    toast("提交成功");
                }
                break;
            case R.id.go_back:
                finish();
                break;
        }
    }

    private void commitExpress() {
        int expressid = bankStrList.get(selected_bank).getPop_id();

        String invoice_no =expressNumbersEt.getText().toString().trim();
        String url=""+MainActivity.url+"act=goods&op=select_express&member_id="
                + MainActivity.myLogin.getMemberid()+"&return_id="+Refund_id+"&express_id="+expressid+"&invoice_no="+invoice_no+"";
        Log.i("zhangxiaoxidebug", "url=" + url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            //{"code":200,"data":"\u63d0\u4ea4\u6210\u529f"}
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if(result==200){
                                String data=resultObject.optString("data");
                                toast(data);
                                setResult(2);
                                finish();
                            }


                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {


                    }
                });
    }
}
